/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

//#include "ti/driverlib/dl_gpio.h"
//#include "ti/driverlib/m0p/dl_core.h"

#include "ti_msp_dl_config.h"
#include "oled.h"
#include "clock.h"
#include "bmp.h"
#include <stdio.h>
#include "Motor.h"
#include "PID_Angle_IR_Speed.h"
#include "Xunxian.h"
#include "grayscale.h"
#include "no_mcu.h"



char text[15] = {0};

uint8_t x1,x2,x3,x4,x5,x6,x7,x8 = 0;
uint8_t sensor_pattern = 0;

PID_Struct Motor_PID_left;
PID_Struct Motor_PID_Right;
float PID_SpeedL_Out = 0;
float PID_SpeedR_Out = 0;


PIDController PID_IR;
float PID_IR_Out = 0;
int err;


uint8_t L_Zhuanwan = 0;
uint8_t R_Zhuanwan = 0;

extern No_MCU_Sensor sensor;

unsigned int adc_getValue(void);
void uart0_send_char(char ch)
{
    //当串口0忙的时候等待，不忙的时候再发送传进来的字符
    while( DL_UART_isBusy(UART_0_INST) == true );
    //发送单个字符
    DL_UART_Main_transmitData(UART_0_INST, ch);
}
//串口发送字符串
void uart0_send_string(char* str)
{
    //当前字符串地址不在结尾 并且 字符串首地址不为空
    while(*str!=0&&str!=0)
    {
        //发送字符串首地址中的字符，并且在发送完成之后首地址自增
        uart0_send_char(*str++);
    }
}



int main(void)
{
    SYSCFG_DL_init();

    Motor_Init();
    OLED_Init();		//初始化OLED
    OLED_Clear();

    sensor_init();
    // Motor_Angle_PID_Init_Left();
    // Motor_Angle_PID_Init_Right();

    // PID_IR_Init(&PID_IR,8,0,0.001,20);
    // PID_Angle_Init(&PID_Angle,1,0,0.2,20);



    
    while(1) 
    {		
        Xunxian();
        
        read_data();

        // sprintf(text,"%d %d %d %d %d %d %d %d",sensor.Analog_value[0],sensor.Analog_value[1],sensor.Analog_value[2],sensor.Analog_value[3],sensor.Analog_value[4],sensor.Analog_value[5],sensor.Analog_value[6],sensor.Analog_value[7]);
        // uart0_send_string(text);   


        x8 = (sensor.Digtal>>7)&0x01;
        x7 = (sensor.Digtal>>6)&0x01;
        x6 = (sensor.Digtal>>5)&0x01;
        x5 = (sensor.Digtal>>4)&0x01;
        x4 = (sensor.Digtal>>3)&0x01;
        x3 = (sensor.Digtal>>2)&0x01;
        x2 = (sensor.Digtal>>1)&0x01;
        x1 = (sensor.Digtal>>0)&0x01;

        // PID_IR_Out = PID_IR_Compute(&PID_IR,0,err);
        // Motor_PID_left.SetValue  = 50 - PID_IR_Out;
        // Motor_PID_Right.SetValue = 50 + PID_IR_Out;
        // Motor_PID_left.SetValue  = 40 + err;
        // Motor_PID_Right.SetValue = 40 - err;
        if(L_Zhuanwan == 1)
        {
            A_Motor_ctrl(forward, 1);
            B_Motor_ctrl(forward, 200);
        }
        else if(L_Zhuanwan == 0)
        {
            A_Motor_ctrl(forward, 300 + err*8);
            B_Motor_ctrl(forward, 300 - err*8);
        }
        

        
        // A_Motor_ctrl(forward, 300 + PID_IR_Out);
        // B_Motor_ctrl(forward, 300 - PID_IR_Out);

        // Motor_Angle_PID_Cal_Left();
        // Motor_Angle_PID_Cal_Right();
        // A_Motor_ctrl(forward, Motor_PID_left.Out);
        // B_Motor_ctrl(forward, Motor_PID_Right.Out);

        // sprintf(text, "%f\r\n",PID_IR_Out);
        // uart0_send_string(text);


        sprintf(text, "%d%d%d%d%d%d%d%d",x1,x2,x3,x4,x5,x6,x7,x8);
        // uart0_send_string(text);
        OLED_ShowString(0,4,(uint8_t *)text,16);

    }
}


// unsigned int adc_getValue(void)
// {
//     unsigned int gAdcResult = 0;

//     //使能ADC转换
//     DL_ADC12_enableConversions(ADC_VOLTAGE_INST);
//     //软件触发ADC开始转换
//     DL_ADC12_startConversion(ADC_VOLTAGE_INST);

//     //如果当前状态 不是 空闲状态
//     while (DL_ADC12_getStatus(ADC_VOLTAGE_INST) != DL_ADC12_STATUS_CONVERSION_IDLE );

//     //清除触发转换状态
//     DL_ADC12_stopConversion(ADC_VOLTAGE_INST);
//     //失能ADC转换
//     DL_ADC12_disableConversions(ADC_VOLTAGE_INST);

//     //获取数据
//     gAdcResult = DL_ADC12_getMemResult(ADC_VOLTAGE_INST, ADC_VOLTAGE_ADCMEM_ADC_CH0);

//     return gAdcResult;
// }